<?php


namespace app\admin\logic;


use app\common\logic\BaseLogic;
use app\common\model\mysql\UserInviteModel;
use app\common\model\mysql\UserLevelModel;
use app\common\model\mysql\UserModel;

class UserLogic extends BaseLogic
{
    public function index($request)
    {
        $search = $request->get();

        $where = [];
        if (!empty($search['name'])) {
            $where[] = ['nickname', 'LIKE', "%{$search['name']}%"];
        }
        if (!empty($search['mobile'])) {
            $where[] = ['mobile', '=', $search['mobile']];
        }

        //显示列表
        $list = UserModel::getPaginate($where, [], 20, ['id' => 'DESC'], ['query' => $search]);

        foreach ($list as &$value) {
            $value['login_time'] = date('Y-m-d H:i:s', $value['login_time']);
            $pid = UserInviteModel::getAlone(['user_id' => $value['id']], 'pid_id');
            $value['pid_name'] = '';
            if ($pid) {
                $value['pid_name'] = UserModel::getAlone(['id' => $pid], 'nickname');
            }
        }
        $level = UserLevelModel::getList([], ['id', 'name']);

        return view('User/user', compact('list', 'search', 'level'));
    }


    public function read($request, $id)
    {
        $search = $request->get();
        $userIds = UserInviteModel::getColumn(['pid_id' => $id], 'user_id');
        $where[] = ['id', 'in', $userIds];
        if (!empty($search['name'])) {
            $where[] = ['nickname', 'LIKE', "%{$search['name']}%"];
        }
        if (!empty($search['mobile'])) {
            $where[] = ['mobile', '=', $search['mobile']];
        }
        //显示列表
        $list = UserModel::getPaginate($where, [], 20, ['id' => 'DESC'], ['query' => $search]);

        foreach ($list as &$value) {
            $value['login_time'] = date('Y-m-d H:i:s', $value['login_time']);
        }
        $level = UserLevelModel::getList([], ['id', 'name']);

        return view('User/user_invite', compact('list', 'search', 'level'));
    }

    public function renew($request, $id)
    {
        $post = $request->post();

        $res = UserModel::editData(['level_id' => $post['level_id']], ['id' => $id]);

        if (!$res) return $this->responseError('操作失败');

        return $this->responseSuccess('操作成功');

    }

    public function export($request)
    {
        $search = $request->get();

        $where = [];
        if (!empty($search['name'])) {
            $where[] = ['nickname', 'LIKE', "%{$search['name']}%"];
        }
        if (!empty($search['mobile'])) {
            $where[] = ['mobile', '=', $search['mobile']];
        }

        //显示列表
        $list = UserModel::getList($where);

        foreach ($list as &$value) {
            $value['login_time'] = date('Y-m-d H:i:s', $value['login_time']);

        }
        $level = UserLevelModel::getList([], ['id', 'name']);

        $tbody = [];
        $thead = ['昵称', '性别', '手机号', '段位', '积分', '累计积分', '最后登入时间', '加入时间'];
        foreach ($list as $key => $item) {
            $sex = '未知';
            if ($item['sex'] == 1) {
                $sex = '男';
            }
            if ($item['sex'] == 2) {
                $sex = '女';
            }
            $levelName = '';
            foreach ($level as $value) {
                if ($item['level_id'] == $value['id']) {
                    $levelName = $value['name'];
                }
            }
            $tbody_tmp = [];
            $tbody_tmp[] = $item['nickname'];
            $tbody_tmp[] = $sex;
            $tbody_tmp[] = $item['mobile'];
            $tbody_tmp[] = $levelName;
            $tbody_tmp[] = $item['integral'];
            $tbody_tmp[] = $item['integral_add'];
            $tbody_tmp[] = $item['login_time'];
            $tbody_tmp[] = $item['create_time'];
            $tbody[] = $tbody_tmp;
        }
        // 导出文件
        $filePath = env('runtime_path') . 'user_' . date('Ymd') . '.csv';
        exportCsv($filePath, $thead, $tbody);
    }
}